package pers.mine.frontUser.service.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pers.mine.dao.FrontCommentMapper;
import pers.mine.dao.QueryMapper;
import pers.mine.frontUser.service.FrontCommentService;
import pers.mine.nook.PageInfo;
import pers.mine.nook.ParamBag;
import pers.mine.nook.QueryHandler;
import pers.mine.nook.SysInfo;
import pers.mine.po.FrontComment;
import pers.mine.po.FrontUser;
import pers.mine.utils.MineStringUtils;

/**
 * 前台评论服务实现
 * @author Mine
 * @since 2017年5月29日_上午11:17:29
 */
@Service
public class FrontCommentServiceImpl extends BaseServiceImpl<FrontComment> implements
		FrontCommentService {
	private static final Log logger = LogFactory.getLog(FrontCommentServiceImpl.class);
	@Autowired
	private QueryMapper queryMapper; 
	
	private FrontCommentMapper frontCommentMapper; 
	@Autowired
	public void setFrontCommentMapper(FrontCommentMapper frontCommentMapper) {
		this.frontCommentMapper = frontCommentMapper;
		super.setBaseMapper(frontCommentMapper);//给BaseServiceImpl赋值 
	}

	@Override
	public PageInfo<FrontComment> findSomeByQueryHandler(
			QueryHandler queryHandler) throws Exception {
		List<FrontComment> list = queryMapper.queryFrontComment(queryHandler);
		long rootSize = 0;
		int nowPage = 1;
		int pageSize = SysInfo.PAGE_INFO_DEFAULT_SIZE;
		if (queryHandler == null || queryHandler.getLimitSentence() == null) {// 没分页
			if (logger.isDebugEnabled()) {
				logger.debug("构造PageInfo:未指定分页信息...");
			}
			if (list != null) {
				rootSize = list.size();
			}
		} else {
			nowPage = queryHandler.getPageNumber();
			pageSize = queryHandler.getPageSize();
			// 获取总记录数
			rootSize = queryMapper.findLastRows();
			if (logger.isDebugEnabled()) {
				logger.debug("构造PageInfo,分页信息:rootSize=" + rootSize
						+ ",nowPage=" + nowPage + ",pageSize=" + pageSize);
			}
		}
		PageInfo<FrontComment> pageInfo = new PageInfo<FrontComment>((int) rootSize,
				nowPage, pageSize);
		pageInfo.setBasePageList(list);
		return pageInfo;
	}

	@Override
	public PageInfo<FrontComment> findSomeByVid(String vid, ParamBag info)
			throws Exception {
		if(MineStringUtils.IsNullOrWhiteSpace(vid)
				||info==null
				||info.getNowPage()==null
				||info.getPageSize()==null){
			throw new Exception("参数有误");
		}
		Integer nowPage = info.getNowPage();
		Integer size = info.getPageSize();
		if(nowPage<0){
			nowPage =1;
		}
		if(size<1){
			size =SysInfo.PAGE_INFO_DEFAULT_SIZE;
		}
		long rootSize = 0;
		List<FrontComment> list = frontCommentMapper.selectByVidWithUserInfo(vid, (nowPage-1)*size, size);
		rootSize = queryMapper.findLastRows();
		PageInfo<FrontComment> pageInfo = new PageInfo<FrontComment>((int) rootSize,
				nowPage, size);
		pageInfo.setBasePageList(list);
		return pageInfo;
	}

}
